package net.hmr_radio.hmrbot.controller.bot.create;

import net.hmr_radio.hmrbot.controller.BaseLoggedInController;
import net.hmr_radio.hmrbot.model.Bot;
import net.hmr_radio.hmrbot.service.BotService;

import org.slim3.controller.Navigation;
import org.slim3.controller.validator.RequiredValidator;
import org.slim3.controller.validator.Validators;
import org.slim3.util.BeanUtil;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class PostController extends BaseLoggedInController {

    private UserService userService = UserServiceFactory.getUserService();

    private BotService botService = new BotService();

    @Override
    public Navigation doRun() throws Exception {
        Validators v = new Validators(request);
        v.add("name", RequiredValidator.INSTANCE);
        if (!v.validate()) {
            return forward("index.jsp");
        }

        Bot bot = new Bot();
        BeanUtil.copy(request, bot);
        bot.setOwnerId(userService.getCurrentUser().getUserId());

        botService.save(bot);

        sessionScope("auth", bot);
        sessionScope("backPath", "/bot/create/success");
        return forward("/twitter/auth");
    }
}
